//
//  LTHomeViewController.swift
//  微博08
//
//  Created by 123 on 16/2/29.
//  Copyright © 2016年 Tsr. All rights reserved.
//

import UIKit

class LTHomeViewController: LTBaseTableViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigationBar()
    }
    
    // 设置导航栏
    func setupNavigationBar() {
        setupBarButtonItem()
        setupTitleView()
    }
    
    // 设置导航栏左右item
    func setupBarButtonItem() {
        // 创建左边BarButton
        // 创建按钮
        /*
        方式一:
        let leftButton = UIButton()
        leftButton.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
        leftButton.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
        leftButton.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
        
        // 设置右边BarButton
        let rightButton = UIButton()
        rightButton.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
        rightButton.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
        rightButton.sizeToFit()
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
        */
        
        /*
        方式二
        navigationItem.leftBarButtonItem = UIBarButtonItem.ceareBarButtonItem(imageName: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem.ceareBarButtonItem(imageName: "navigationbar_pop")
        */
        
        // 创建左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        // 创建右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
    }
    
    // MARK: - 按钮点击事件
    func titleButtonClick(button: UIButton) {
        
        // 根据按钮的selected来确定按钮旋转的角度
        // selected = false, 箭头向下
        // selected = true, 箭头向上
        button.selected = !button.selected
//        if button.selected {
//            UIView.animateWithDuration(0.25, animations: { () -> Void in
//                button.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001))
//            })
//        } else {
//            UIView.animateWithDuration(0.25, animations: { () -> Void in
//                button.imageView?.transform = CGAffineTransformIdentity
//            })
//        }
        
        UIView.animateWithDuration(0.25) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
        
        // 点击选中按钮
        if button.selected {
            // 从xib中加载sb
            let sb = UIStoryboard(name: "Popover", bundle: nil)
            
            // 创建控制器
            let popoverVC = sb.instantiateViewControllerWithIdentifier("popover")
            
            // 在iPhone上面设置Popover默认会适配成Modal
            popoverVC.modalPresentationStyle = UIModalPresentationStyle.Popover
            
            // 设置popoverPresentationController代理
            popoverVC.popoverPresentationController?.delegate = self
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            
            // 设置弹出控制器显示的大小
            popoverVC.preferredContentSize = CGSize(width: 200, height: 5 * 44)
            
            
            // 将加载好的popoverVC显示
            presentViewController(popoverVC, animated: true, completion: nil)
        }
    }
    
    func setupTitleView() {
        // 创建按钮
        let titleBtutton = LTTitleButton(title: "我是标题")
        
        // 按钮点击事件
        titleBtutton.addTarget(self, action: "titleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleBtutton
    }
}

extension LTHomeViewController: UIPopoverPresentationControllerDelegate {
    // 让iPhone上面可以显示popover效果
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
        let button = navigationItem.titleView as! LTTitleButton
        titleButtonClick(button)
    }
}
